package 宝贝复制;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.ParserConfig;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.ItemAddRequest;
import com.taobao.api.request.ItemSellerGetRequest;
import com.taobao.api.request.ItemUpdateRequest;
import com.taobao.api.response.ItemAddResponse;
import com.taobao.api.response.ItemSellerGetResponse;
import com.taobao.api.response.ItemUpdateResponse;

import java.io.*;

public class ZTest {


    private static String URL = "http://gw.api.taobao.com/router/rest";

    private static String APP_KEY = "21404171";

    private static String APP_SECRET = "724576dc06e80ed8e38d1ad2f6de39da";

    private static String TZG_APP_KEY = "21255586";

    private static String TZG_APP_SECRET = "04eb2b1fa4687fbcdeff12a795f863d4";

    private static String JIAOYI_APP_KEY = "23256680";

    private static String JIAOYI_APP_SECRET = "06291fbd4cd886ab5723204bc399855a";

    private static String USER_NICK = "liu405607850";

    private static String SESSIONKEY = "61020252ea187f57b2dc72603fd40714d14b58egdb83d252881213009";

    private static Long TID = 41347369313696290L;

    private static String CONSUMERID = "CID_test_lzl";

    private static String TOPIC_NAMES = "taobao_trade_TradeRated";

    private static String fields = "user_id,uid,nick,seller_credit,status,has_shop,type";

    static String clorest510sid = "6101609f77d30c7d017d07360f264326528448f7eee0e6179742176";

    public static void main(String[] args) throws Exception {

//        ItemUpdateRequest();

//        ItemAddRequest();
        System.out.println(1 >> 1);

    }


    public static void ItemSellerGetRequest() throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(URL, TZG_APP_KEY, TZG_APP_SECRET);
        ItemSellerGetRequest req = new ItemSellerGetRequest();
        req.setFields("seller_cids,detail_url,approve_status,num_iid,title,wireless_desc,"
                + "nick,cid,pic_url,num,price,list_time,delist_time,outer_id,desc,modified,is_fenxiao,is_virtual");
        req.setNumIid(563315709894L);
        ItemSellerGetResponse rsp = client.execute(req, clorest510sid);
        System.out.println(rsp.getBody());
    }

    public static void ItemUpdateRequest() throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(URL, TZG_APP_KEY, TZG_APP_SECRET);
        ItemUpdateRequest req = new ItemUpdateRequest();
        req.setNumIid(563315709894L);
        ItemUpdateResponse rsp = client.execute(req, clorest510sid);
        System.out.println(rsp.getBody());
    }

    public static void ItemAddRequest() throws IOException, ApiException {
        TaobaoClient client = new DefaultTaobaoClient(URL, TZG_APP_KEY, TZG_APP_SECRET);
        String json = readStringFile("C:\\Users\\User\\IdeaProjects\\zql\\src\\main\\resources\\tm宝贝复制\\2018-01-08\\测试输出1.json");
        // 配置fastJson的反序列化策略
        ParserConfig parserConfig = new ParserConfig();
        parserConfig.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;

        JSONObject jsonObject = JSON.parseObject(json);
        ItemAddRequest req = jsonObject.getObject("params", ItemAddRequest.class);
        req.setLocationState("福建");
        req.setLocationCity("南平");


        ItemAddResponse execute = client.execute(req, clorest510sid);

        FileUtil.print("测试输出", JSON.toJSONString(execute));
    }

    private static String readStringFile(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) throw new RuntimeException("文件不存在 " + filePath);

        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line).append("\r\n");
        }
        reader.close();

        return stringBuilder.toString();
    }


}
